/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufrn.adurn.architecture.dao;

import br.ufrn.adurn.domain.Professor;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Victor
 */
public class ProfessorDAO {

    private Connection connection;

    public ProfessorDAO() {
        this.connection = new ConnectionFactory().getConnection();
    }

    public void adiciona(Professor professor) {
        String sql = "INSERT INTO professores (nome, cpf, data_nasc, logradouro, complemento,"
                + " bairro, cep, cidade, uf, email, newsletter, tel1, tel2, end_valido, mala_direta, observacoes, id_depto, siape,"
                + " titulacao, categoria, nivel, ch, is_ativo, is_aposent, is_conselho, is_diretoria, is_afastado, is_falecido,"
                + " is_possui_pens, is_pendencia, vb, contrib, is_sind, is_consig, dataEntrUFRN, dataSind, dataAtualizacao, usuario) "
                + "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

        try {
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setString(1, professor.getNome());
            stmt.setString(2, professor.getCpf());
            //stmt.setDate(3, new Date(professor.getDataNasc().getTimeInMillis()));
            stmt.setDate(3, null);
            stmt.setString(4, professor.getLogradouro());
            stmt.setString(5, professor.getComplemento());
            stmt.setString(6, professor.getBairro());
            stmt.setString(7, professor.getCep());
            stmt.setString(8, professor.getCidade());
            stmt.setString(9, professor.getUf());
            stmt.setString(10, professor.getEmail());
            stmt.setBoolean(11, professor.isNewsletter());
            stmt.setString(12, professor.getTel1());
            stmt.setString(13, professor.getTel2());
            stmt.setBoolean(14, professor.isEnderecoValido());
            stmt.setBoolean(15, professor.isMalaDireta());
            stmt.setString(16, professor.getObservacoes());
            stmt.setLong(17, professor.getIdDepto());
            stmt.setString(18, professor.getSiape());
            stmt.setString(19, professor.getTitulacao());
            stmt.setString(20, professor.getCategoria());
            stmt.setString(21, professor.getNivel());
            stmt.setString(22, professor.getCargaHoraria());
            stmt.setBoolean(23, professor.isIsAtivo());
            stmt.setBoolean(24, professor.isIsAposentado());
            stmt.setBoolean(25, professor.isIsConselho());
            stmt.setBoolean(26, professor.isIsDiretoria());
            stmt.setBoolean(27, professor.isIsAfastado());
            stmt.setBoolean(28, professor.isIsFalecido());
            stmt.setBoolean(29, professor.isIsPossuiPensionista());
            stmt.setBoolean(30, professor.isIsPendencia());
            stmt.setDouble(31, professor.getVb());
            stmt.setDouble(32, professor.getContribuicao());
            stmt.setBoolean(33, professor.isIsSindicalizado());
            stmt.setBoolean(34, professor.isIsConsignado());
//            stmt.setDate(32, new Date(professor.getDataEntradaUFRN().getTimeInMillis()));
//            stmt.setDate(33, new Date(professor.getDataSind().getTimeInMillis()));
            stmt.setDate(35, null);
            stmt.setDate(36, null);
            stmt.setDate(37, null);
            stmt.setString(38, null);
            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        System.out.println(professor.getNome() + " adicionado com sucesso");
    }

    //public Professor buscaByNome(String nome); BUSCA POR NOME, CPF OU SIAPE
    public List<Professor> buscaByNome(String nome) {
        nome = nome.trim(); // remover espacos em branco
        List<Professor> professores = new ArrayList<Professor>();
        try {
            PreparedStatement stmt = this.connection.prepareStatement("select * from professores where nome like '" + nome + "%'");
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                Professor professor = new Professor();
                professor.setNome(rs.getString("nome"));
                professor.setSiape(rs.getString("siape"));
                professor.setCpf(rs.getString("cpf"));
                professores.add(professor);
            }
            rs.close();
            stmt.close();
            return professores;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void remove(Professor professor) {
//        try {
//            PreparedStatement stmt = connection.prepareStatement("delete from professors where id=?");
//            stmt.setLong(1, professor.getIdProfessor());
//            stmt.execute();
//            stmt.close();
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
    }
}
